args = "n"
args=cstr(args)
specifiedChars = "z,e,t,b"
Dim doc
Set doc = application.ActiveDocument
GetUserLicence(doc)
main
sub main()
	args = lcase(trim(args))
	if args = "" then exit sub
	if args="t" then args = "1"
	if args="b" then args = doc.layercount
	
	InvisibleAll
	InvisibleAllFabItems
	InvisibleAllUserlayer
	dim activelyr
	activelyr = ExtractFirstNumber(args)
	if IsNumeric(activelyr)=true then 
		application.gui.activemode =1 'route
		application.gui.activeroutelayer =activelyr
	end if
	'show etch elements
	if ContainsDigitsOrSpecifiedChars(args,specifiedChars)=true then
		VisbleBoardElements
		VisbleEtchElements	
	end if
	
	dim s,ls,i,j,selitem
	s= split(args," ")
	ls = ubound(s)-lbound(s)+1
	
	
	for i= 0 to ls-1
		selitem = trim(s(i))
		if cstr(selitem)="t" then selitem = 1
		if cstr(selitem)="b" then selitem = doc.layercount		
		if IsNumeric(selitem)=true then
			'etch layer
			call VisibleSelectedEtchLayer(selitem)
			if selitem = 1 then
				doc.ActiveView.DisplayControl.GeneralItems(6) = true'epcbDCFiducialsTop  
				doc.ActiveView.DisplayControl.PartItems(22,epcbSideTop)= true 'epcbDCTestPointPads
			end if
			if selitem = doc.layercount then
				doc.ActiveView.DisplayControl.GeneralItems(5) = true'epcbDCFiducialsBottom   
				doc.ActiveView.DisplayControl.PartItems(22,epcbSideBottom)= true 'epcbDCTestPointPads
			end if
		else
			'else
			select case selitem
				case "n"	
					InvisibleAll
				case "e"	
					doc.ActiveView.DisplayControl.GeneralItems(6) = true'epcbDCFiducialsTop  
					doc.ActiveView.DisplayControl.PartItems(22,epcbSideTop)= true 'epcbDCTestPointPads
					doc.ActiveView.DisplayControl.GeneralItems(5) = true'epcbDCFiducialsBottom   
					doc.ActiveView.DisplayControl.PartItems(22,epcbSideBottom)= true 'epcbDCTestPointPads
					for j=1 to doc.layercount
						call VisibleSelectedEtchLayer(j)						
					next
				case "pmt"	
					InvisibleAll
					InvisibleAllFabItems
					InvisibleAllUserlayer
					doc.ActiveView.DisplayControl.EnableSection(19)=true
					doc.ActiveView.DisplayControl.GeneralItems(17) = true
				case "pmb"
					InvisibleAll
					InvisibleAllFabItems
					InvisibleAllUserlayer
					doc.ActiveView.DisplayControl.EnableSection(19)=true
					doc.ActiveView.DisplayControl.GeneralItems(14) = true
				case "smt"
					InvisibleAll
					InvisibleAllFabItems
					InvisibleAllUserlayer
					doc.ActiveView.DisplayControl.EnableSection(19)=true					
					doc.ActiveView.DisplayControl.GeneralItems(15) = true
				case "smb"
					InvisibleAll
					InvisibleAllFabItems
					InvisibleAllUserlayer
					doc.ActiveView.DisplayControl.EnableSection(19)=true
					doc.ActiveView.DisplayControl.GeneralItems(16) = true
				case "sst"
					doc.ActiveView.DisplayControl.PartItems (17,epcbSideTop) = true
				case "ssb"
					doc.ActiveView.DisplayControl.PartItems (17,epcbSideBottom) = true
				case "adt"
					doc.ActiveView.DisplayControl.PartItems (0,epcbSideTop) = true
				case "adb"
					doc.ActiveView.DisplayControl.PartItems (0,epcbSideBottom) = true
				case "pt"
					doc.ActiveView.DisplayControl.PartItems (13,epcbSideTop) = true					
				case "pb"
					doc.ActiveView.DisplayControl.PartItems (13,epcbSideBottom) = true
				case "o"
					InvisibleAll
					InvisibleAllFabItems
					InvisibleAllUserlayer
					application.gui.activemode =1 'route
					application.gui.activeroutelayer =1
					VisbleBoardElements
					VisbleEtchElements	
					call VisibleSelectedEtchLayer(1)	
					call VisibleSelectedEtchLayer(doc.layercount)					
					'doc.ActiveView.DisplayControl.UserLayer ("DXF_Top") = true		
					'doc.ActiveView.DisplayControl.UserLayer("DXF_BOT") = true
					'doc.ActiveView.DisplayControl.GeneralItems(15) = true
					'doc.ActiveView.DisplayControl.GeneralItems(16) = true	
				case "i"
					InvisibleAll
					InvisibleAllFabItems
					InvisibleAllUserlayer
					application.gui.activemode =1 'route
					application.gui.activeroutelayer =2
					VisbleBoardElements
					VisbleEtchElements	
					for k=2 to doc.layercount-1
						call VisibleSelectedEtchLayer(k)
					next
				case "td"
					InvisibleAll
					InvisibleAllFabItems
					InvisibleAllUserlayer
					application.gui.activemode =1 'route
					application.gui.activeroutelayer =1
					VisbleBoardElements
					VisbleEtchElements	
					call VisibleSelectedEtchLayer(1)							
					doc.ActiveView.DisplayControl.UserLayer ("DXF_Top") = true
				case "bd"
					InvisibleAll
					InvisibleAllFabItems
					InvisibleAllUserlayer
					application.gui.activemode =1 'route
					application.gui.activeroutelayer =doc.layercount
					VisbleBoardElements
					VisbleEtchElements
					call VisibleSelectedEtchLayer(doc.layercount)			
					doc.ActiveView.DisplayControl.UserLayer("DXF_BOT") = true
				case "ts"
					InvisibleAll
					InvisibleAllFabItems
					InvisibleAllUserlayer
					application.gui.activemode =1 'route
					application.gui.activeroutelayer =1
					VisbleBoardElements
					VisbleEtchElements
					call VisibleSelectedEtchLayer(1)
					doc.ActiveView.DisplayControl.GeneralItems(15) = true
				case "bs"
					InvisibleAll
					InvisibleAllFabItems
					InvisibleAllUserlayer
					application.gui.activemode =1 'route
					application.gui.activeroutelayer =doc.layercount
					VisbleBoardElements
					VisbleEtchElements
					call VisibleSelectedEtchLayer(doc.layercount)
					doc.ActiveView.DisplayControl.GeneralItems(16) = true
			end select
		end if
	next
	
	
	if IsContainsSpecifiedChars(args,"1,t")=false then
		doc.ActiveView.DisplayControl.GeneralItems(6) = false'epcbDCFiducialsTop  
		doc.ActiveView.DisplayControl.PartItems(22,epcbSideTop)= false 'epcbDCTestPointPads
	end if
	if IsContainsSpecifiedChars(args,"b,"& doc.layercount)=false then
		doc.ActiveView.DisplayControl.GeneralItems(5) = false'epcbDCFiducialsTop  
		doc.ActiveView.DisplayControl.PartItems(22,epcbSideBottom)= false 'epcbDCTestPointPads
	end if
end sub
sub InvisibleAllUserlayer()
	'on error resume next
	doc.ActiveView.DisplayControl.EnableSection(19)=true 'epcbDCUserDraftLayersSection.		
	dim ulyrs,ulyr,i
	set ulyrs = doc.UserLayers
	for each ulyr in ulyrs
		'msgbox ulyr.name
		doc.ActiveView.DisplayControl.UserLayer(ulyr.name) = false
	next
end sub
sub InvisibleAllFabItems()
	doc.ActiveView.DisplayControl.EnableSection(19)=true'epcbDCFabricationSection 

	doc.ActiveView.DisplayControl.GeneralItems(14) = false
	doc.ActiveView.DisplayControl.GeneralItems(15) = false
	doc.ActiveView.DisplayControl.GeneralItems(16) = false
	doc.ActiveView.DisplayControl.GeneralItems(17) = false
end sub

Function ExtractFirstNumber(inputStr)
    ' 分割字符串为数组
    Dim parts, i
    parts = Split(Trim(inputStr), " ")
    
    ' 遍历所有部分
    For i = 0 To UBound(parts)
        If Len(parts(i)) > 0 Then  ' 忽略空元素
            ' 检查是否包含数字
            If IsNumeric(parts(i)) Then
                ' 额外验证是否为纯数字（避免科学计数法）
                If parts(i) = CStr(CLng(parts(i))) Then
                    ExtractFirstNumber = parts(i)
                    Exit Function
                End If
            End If
        End If
    Next
    
    ' 如果未找到数字则返回空字符串
    ExtractFirstNumber = ""
End Function
sub InvisibleAll()
	on error resume next
	dim i
	for i=0 to 24
		doc.ActiveView.DisplayControl.EnableSection(i)=false
	next
	
	for i=1 to doc.layercount
		doc.ActiveView.DisplayControl.ConductorLayer(i,0)=false
		doc.ActiveView.DisplayControl.ConductorLayer(i,1)=false
	next	
end sub
Function ContainsDigitsOrSpecifiedChars(inputStr, specifiedChars)
	
    ' 检查是否包含数字
    Dim regex, hasDigit
    Set regex = New RegExp
    regex.Pattern = "\d"  ' 匹配任意数字
    hasDigit = regex.Test(inputStr)
    
    ' 检查是否包含指定字符
    Dim hasSpecifiedChar, char
    hasSpecifiedChar = False
    For Each char In Split(specifiedChars, ",")
        If InStr(1, inputStr, Trim(char), vbTextCompare) > 0 Then
            hasSpecifiedChar = True
            Exit For
        End If
    Next
    
    ' 返回结果
    ContainsDigitsOrSpecifiedChars = hasDigit Or hasSpecifiedChar
End Function

Function IsContainsSpecifiedChars(inputStr,specifiedChars)	
    ' 检查是否包含指定字符
    Dim hasSpecifiedChar, char
    hasSpecifiedChar = False
    For Each char In Split(specifiedChars, ",")
        If InStr(1, inputStr, Trim(char), vbTextCompare) > 0 Then
            hasSpecifiedChar = True
            Exit For
        End If
    Next
    
    ' 返回结果
    IsContainsSpecifiedChars = hasSpecifiedChar
End Function
sub VisbleBoardElements()
	doc.ActiveView.DisplayControl.EnableSection(4)=true 'epcbDCBoardItemsSection 
	
	doc.ActiveView.DisplayControl.GeneralItems(0) = true'epcbDCBoardOrigin
	doc.ActiveView.DisplayControl.GeneralItems(1) = true'epcbDCBoardOutline 
	doc.ActiveView.DisplayControl.GeneralItems(2) = true'epcbDCContours 
	doc.ActiveView.DisplayControl.GeneralItems(11) = true'epcbDCRouteBorder  
	doc.ActiveView.DisplayControl.GeneralItems(9) = true'epcbDCOtherHoles  
	doc.ActiveView.DisplayControl.GeneralItems(7) = true'epcbDCMountingHoles  
	doc.ActiveView.DisplayControl.GeneralItems(25) = true'epcbDCManufacturingOutline 	
	doc.ActiveView.DisplayControl.MiscItems (epcbDCActiveLayerOnly)= false
end sub

sub VisbleEtchElements()
	doc.ActiveView.DisplayControl.EnableSection(7)=true
	doc.ActiveView.DisplayControl.EnableSection(10)=true 'epcbDCPadsSection 
	doc.ActiveView.DisplayControl.EnableSection(16)=true 'epcbDCTracesSection 
	
	doc.ActiveView.DisplayControl.MiscItems(11)=true 'epcbDCThroughPads 
	doc.ActiveView.DisplayControl.MiscItems(12)=true 'epcbDCThroughVias 
end sub

sub VisibleSelectedEtchLayer(oid)
	doc.ActiveView.DisplayControl.ConductorLayer(oid,0)=TRUE
	doc.ActiveView.DisplayControl.ConductorLayer(oid,1)=TRUE	
end sub


'###########################################
'###########################################
'###########################################
' Function to validate document
Private Function ValidateServer(docObj)
    Dim key, licenseServer, licenseToken
    key = docObj.Validate(0)
    Set licenseServer = CreateObject("MGCPCBAutomationLicensing.Application")
    licenseToken = licenseServer.GetToken(key)
    Set licenseServer = nothing
    'On Error Resume Next
    Err.Clear
    docObj.Validate(licenseToken)
    If Err Then
        ValidateServer = 0    
    Else 
        ValidateServer = 1
    End If
End Function
sub GetUserLicence(docObj)
	If ValidateServer(docObj) = 0 Then
	    msgbox "Server validation failed."
	    exit sub
	End If	
end sub



